package report

import (
	"../common"
	"../web"
	"encoding/json"
	"strconv"
)

type QueryWoPassObj struct {
	common.BaseHandler
}

type WoPassPageObj struct {
	common.BaseHandler
}

type QueryWoFailObj struct {
	common.BaseHandler
}

type WoFailPageObj struct {
	common.BaseHandler
}

type QueryInprocessObj struct {
	common.BaseHandler
}

type InprocessPageObj struct {
	common.BaseHandler
}

type QueryInFailObj struct {
	common.BaseHandler
}

type InFailPageObj struct {
	common.BaseHandler
}

func (self *QueryWoPassObj) ParameterCheck() bool {
	s_nm := self.Ctx.Params["nm"] // query result data number
	s_wo := self.Ctx.Params["wo"] // work order
	s_st := self.Ctx.Params["st"] // station
	if s_wo == "" || s_st == "" {
		return false
	}

	self.Sqlparas = append(self.Sqlparas, s_nm)
	self.Sqlparas = append(self.Sqlparas, s_wo)
	self.Sqlparas = append(self.Sqlparas, s_st)

	return true
}

func (self *QueryWoPassObj) SqlQueryExecute() {
	s_nm := self.Sqlparas[0].(string)
	s_wo := self.Sqlparas[1].(string)
	s_st := self.Sqlparas[2].(string)
	var wo_ls []interface{}
	if s_st == "Total" {
		wo_ls = *common.FetchAllRowsPtr(
			`SELECT TOP 10 ROW_NUMBER() OVER(ORDER BY A.MODIFYDATE) AS AUTOID, 
					B.WONO, C.ITEM, ISNULL(D.DESCRIP, ''), A.SSN_NO, A.CURR_STATION, A.RESULT, CONVERT(CHAR(23), A.MODIFYDATE, 121)
	           FROM SFC_SSNFLOW A(NOLOCK), SFC_SSN B(NOLOCK), SFC_WO C(NOLOCK), SFC_ITEMS D(NOLOCK) 
	          WHERE B.WONO=?
	            AND A.RESULT='PASS'
	            AND A.SSN_NO=B.SSN_NO
	            AND B.WONO=C.WONO
	            AND C.ITEM=D.ITEM`,
			self.Usr.Conn, new(common.QueryDetail), s_wo)
	} else {
		wo_ls = *common.FetchAllRowsPtr(
			`SELECT TOP 10 ROW_NUMBER() OVER(ORDER BY A.MODIFYDATE) AS AUTOID, 
					B.WONO, C.ITEM, ISNULL(D.DESCRIP, ''), A.SSN_NO, A.CURR_STATION, A.RESULT, CONVERT(CHAR(23), A.MODIFYDATE, 121)
	           FROM SFC_SSNFLOW A(NOLOCK), SFC_SSN B(NOLOCK), SFC_WO C(NOLOCK), SFC_ITEMS D(NOLOCK) 
	          WHERE B.WONO=?
			    AND A.CURR_STATION=?
	            AND A.RESULT='PASS'
	            AND A.SSN_NO=B.SSN_NO
	            AND B.WONO=C.WONO
	            AND C.ITEM=D.ITEM`,
			self.Usr.Conn, new(common.QueryDetail), s_wo, s_st)
	}

	rec_curr, _ := strconv.Atoi(s_nm)
	total_id := rec_curr / 10
	if rec_curr%10 > 0 {
		total_id++
	}

	self.TmplData["wo_ls"] = wo_ls
	self.TmplData["curr_page"] = "1"
	self.TmplData["total_page"] = total_id
	self.TmplData["workorder"] = s_wo
	self.TmplData["station"] = s_st
}

func WoPassDetailHandler(ctx *web.Context) {
	self := new(QueryWoPassObj)
	self.Ctx = ctx
	self.Html = "wopassdetail.html"
	self.Rightlist = []string{"querywo", "controlrun"}
	self.DoQueryUrl(self)
}

func (self *WoPassPageObj) ParameterCheck() bool {
	pageid := self.Ctx.Params["pageid"] // current page
	s_wo := self.Ctx.Params["wo"]       // work order
	s_st := self.Ctx.Params["st"]       // station
	s_id, err := strconv.Atoi(pageid)
	if err != nil {
		return false
	}
	if s_wo == "" || s_st == "" {
		return false
	}

	idx := 10 * (s_id - 1)

	self.Sqlparas = append(self.Sqlparas, s_wo)
	self.Sqlparas = append(self.Sqlparas, s_st)
	self.Sqlparas = append(self.Sqlparas, idx)

	return true
}

func (self *WoPassPageObj) SqlQueryExecute() {
	var wo_ls []interface{}
	s_wo := self.Sqlparas[0].(string)
	s_st := self.Sqlparas[1].(string)
	idx := self.Sqlparas[2].(int)
	if s_st == "Total" {
		wo_ls = *common.FetchAllRowsPtr(
			`SELECT TOP 10 A.AUTOID, A.WONO, A.ITEM, ISNULL(A.DESCRIP, ''), A.SSN_NO, A.CURR_STATION, 
                			A.RESULT, CONVERT(CHAR(23), A.MODIFYDATE, 121)
          	   FROM
             		(
                	SELECT ROW_NUMBER() OVER(ORDER BY A.MODIFYDATE) AS AUTOID, 
							B.WONO, C.ITEM, ISNULL(D.DESCRIP, '') AS DESCRIP, A.SSN_NO, A.CURR_STATION, A.RESULT, CONVERT(CHAR(23), A.MODIFYDATE, 121) AS MODIFYDATE
          			  FROM SFC_SSNFLOW A(NOLOCK), SFC_SSN B(NOLOCK), SFC_WO C(NOLOCK), SFC_ITEMS D(NOLOCK) 
         			 WHERE B.WONO=?
           			   AND A.RESULT='PASS'
           			   AND A.SSN_NO=B.SSN_NO
           			   AND B.WONO=C.WONO
           			   AND C.ITEM=D.ITEM
             		) A
         	  WHERE A.AUTOID > ?`,
			self.Usr.Conn, new(common.QueryDetail), s_wo, idx)
	} else {
		wo_ls = *common.FetchAllRowsPtr(
			`SELECT TOP 10 A.AUTOID, A.WONO, A.ITEM, ISNULL(A.DESCRIP, ''), A.SSN_NO, A.CURR_STATION, 
                			A.RESULT, CONVERT(CHAR(23), A.MODIFYDATE, 121)
          	   FROM
             		(
                	SELECT ROW_NUMBER() OVER(ORDER BY A.MODIFYDATE) AS AUTOID, 
							B.WONO, C.ITEM, ISNULL(D.DESCRIP, '') AS DESCRIP, A.SSN_NO, A.CURR_STATION, A.RESULT, CONVERT(CHAR(23), A.MODIFYDATE, 121) AS MODIFYDATE
          			  FROM SFC_SSNFLOW A(NOLOCK), SFC_SSN B(NOLOCK), SFC_WO C(NOLOCK), SFC_ITEMS D(NOLOCK) 
         			 WHERE B.WONO=?
         			   AND A.CURR_STATION=?
           			   AND A.RESULT='PASS'
           			   AND A.SSN_NO=B.SSN_NO
           			   AND B.WONO=C.WONO
           			   AND C.ITEM=D.ITEM
             		) A
         	  WHERE A.AUTOID > ?`,
			self.Usr.Conn, new(common.QueryDetail), s_wo, s_st, idx)
	}

	dict_js, _ := json.Marshal(map[string]interface{}{"details": wo_ls, "lang": self.Lang})
	self.Ctx.ResponseWriter.Write(dict_js)
}

func SplitWoPassByPageHandler(ctx *web.Context) {
	self := new(WoPassPageObj)
	self.Ctx = ctx
	self.DoJsonQuery(self)
}

func (self *QueryWoFailObj) ParameterCheck() bool {
	s_nm := self.Ctx.Params["nm"] // query result data number
	s_wo := self.Ctx.Params["wo"] // work order
	s_st := self.Ctx.Params["st"] // station
	if s_wo == "" || s_st == "" {
		return false
	}

	self.Sqlparas = append(self.Sqlparas, s_nm)
	self.Sqlparas = append(self.Sqlparas, s_wo)
	self.Sqlparas = append(self.Sqlparas, s_st)

	return true
}

func (self *QueryWoFailObj) SqlQueryExecute() {
	s_nm := self.Sqlparas[0].(string)
	s_wo := self.Sqlparas[1].(string)
	s_st := self.Sqlparas[2].(string)
	var wo_ls []interface{}
	if s_st == "Total" {
		wo_ls = *common.FetchAllRowsPtr(
			`SELECT TOP 50 ROW_NUMBER() OVER(ORDER BY B.SSN_NO, B.MODIFYDATE), A.WONO, B.SSN_NO, B.CURR_STATION, 
					 B.RESULT, ISNULL(B.FAILCODE, ''), ISNULL(B.FAILDESC, ''), CONVERT(CHAR(23), B.MODIFYDATE, 121) 
			   FROM SFC_SSN A(NOLOCK), SFC_SSNFLOW B(NOLOCK)
			  WHERE A.SSN_NO=B.SSN_NO
			    AND A.WONO=?
			    AND B.RESULT='FAIL'`,
			self.Usr.Conn, new(common.WoFailInfo), s_wo)
	} else {
		wo_ls = *common.FetchAllRowsPtr(
			`SELECT TOP 50 ROW_NUMBER() OVER(ORDER BY B.SSN_NO, B.MODIFYDATE), A.WONO, B.SSN_NO, B.CURR_STATION, 
					 B.RESULT, ISNULL(B.FAILCODE, ''), ISNULL(B.FAILDESC, ''), CONVERT(CHAR(23), B.MODIFYDATE, 121) 
			   FROM SFC_SSN A(NOLOCK), SFC_SSNFLOW B(NOLOCK)
			  WHERE A.SSN_NO=B.SSN_NO
			    AND A.WONO=?
			    AND B.RESULT='FAIL'
				AND B.CURR_STATION=?`,
			self.Usr.Conn, new(common.WoFailInfo), s_wo, s_st)
	}

	rec_curr, _ := strconv.Atoi(s_nm)
	total_id := rec_curr / 50
	if rec_curr%50 > 0 {
		total_id++
	}

	self.TmplData["wo_ls"] = wo_ls
	self.TmplData["curr_page"] = "1"
	self.TmplData["total_page"] = total_id
	self.TmplData["workorder"] = s_wo
	self.TmplData["station"] = s_st
}

func WoFailDetailHandler(ctx *web.Context) {
	self := new(QueryWoFailObj)
	self.Ctx = ctx
	self.Html = "wofaildetail.html"
	self.Rightlist = []string{"querywo", "controlrun"}
	self.DoQueryUrl(self)
}

func (self *WoFailPageObj) ParameterCheck() bool {
	pageid := self.Ctx.Params["pageid"] // current page
	s_wo := self.Ctx.Params["wo"]       // work order
	s_st := self.Ctx.Params["st"]       // station
	s_id, err := strconv.Atoi(pageid)
	if err != nil {
		return false
	}
	if s_wo == "" || s_st == "" {
		return false
	}

	idx := 50 * (s_id - 1)

	self.Sqlparas = append(self.Sqlparas, s_wo)
	self.Sqlparas = append(self.Sqlparas, s_st)
	self.Sqlparas = append(self.Sqlparas, idx)

	return true
}

func (self *WoFailPageObj) SqlQueryExecute() {
	var wo_ls []interface{}
	s_wo := self.Sqlparas[0].(string)
	s_st := self.Sqlparas[1].(string)
	idx := self.Sqlparas[2].(int)
	if s_st == "Total" {
		wo_ls = *common.FetchAllRowsPtr(
			`SELECT TOP 50 A.AUTOID, A.WONO, A.SSN_NO, A.CURR_STATION, A.RESULT, A.FAILCODE, 
					 A.FAILDESC, A.MODIFYDATE
          	   FROM
             		(
                	SELECT ROW_NUMBER() OVER(ORDER BY B.SSN_NO, B.MODIFYDATE) AS AUTOID, A.WONO, B.SSN_NO, 
                			B.CURR_STATION, B.RESULT, ISNULL(B.FAILCODE, '') AS FAILCODE, 
                			ISNULL(B.FAILDESC, '') AS FAILDESC, CONVERT(CHAR(23), B.MODIFYDATE, 121) AS MODIFYDATE
			   		  FROM SFC_SSN A(NOLOCK), SFC_SSNFLOW B(NOLOCK)
			  		 WHERE A.SSN_NO=B.SSN_NO
			    	   AND A.WONO=?
			    	   AND B.RESULT='FAIL'
             		) A
         	  WHERE A.AUTOID > ?`,
			self.Usr.Conn, new(common.WoFailInfo), s_wo, idx)
	} else {
		wo_ls = *common.FetchAllRowsPtr(
			`SELECT TOP 50 A.AUTOID, A.WONO, A.SSN_NO, A.CURR_STATION, A.RESULT, A.FAILCODE, 
					 A.FAILDESC, A.MODIFYDATE
          	   FROM
             		(
                	SELECT ROW_NUMBER() OVER(ORDER BY B.SSN_NO, B.MODIFYDATE) AS AUTOID, A.WONO, B.SSN_NO, 
                			B.CURR_STATION, B.RESULT, ISNULL(B.FAILCODE, '') AS FAILCODE, 
                			ISNULL(B.FAILDESC, '') AS FAILDESC, CONVERT(CHAR(23), B.MODIFYDATE, 121) AS MODIFYDATE
			   		  FROM SFC_SSN A(NOLOCK), SFC_SSNFLOW B(NOLOCK)
			  		 WHERE A.SSN_NO=B.SSN_NO
			    	   AND A.WONO=?
			    	   AND B.RESULT='FAIL'
					   AND B.CURR_STATION=?
             		) A
         	  WHERE A.AUTOID > ?`,
			self.Usr.Conn, new(common.WoFailInfo), s_wo, s_st, idx)
	}

	dict_js, _ := json.Marshal(map[string]interface{}{"details": wo_ls, "lang": self.Lang})
	self.Ctx.ResponseWriter.Write(dict_js)
}

func SplitWoFailByPageHandler(ctx *web.Context) {
	self := new(WoFailPageObj)
	self.Ctx = ctx
	self.DoJsonQuery(self)
}

func (self *QueryInprocessObj) ParameterCheck() bool {
	s_nm := self.Ctx.Params["nm"] // query result data number
	s_wo := self.Ctx.Params["wo"] // work order
	s_st := self.Ctx.Params["st"] // station
	s_im := self.Ctx.Params["im"] // item
	if s_wo == "" || s_st == "" || s_im == "" {
		return false
	}

	self.Sqlparas = append(self.Sqlparas, s_nm)
	self.Sqlparas = append(self.Sqlparas, s_wo)
	self.Sqlparas = append(self.Sqlparas, s_st)
	self.Sqlparas = append(self.Sqlparas, s_im)

	return true
}

func (self *QueryInprocessObj) SqlQueryExecute() {
	s_nm := self.Sqlparas[0].(string)
	s_wo := self.Sqlparas[1].(string)
	s_st := self.Sqlparas[2].(string)
	s_im := self.Sqlparas[3].(string)
	var wo_ls []interface{}
	if s_wo != "All" {
		if s_st == "All" {
			wo_ls = *common.FetchAllRowsPtr(
				`SELECT TOP 10 ROW_NUMBER() OVER(ORDER BY A.SSN_NO) AS AUTOID, 
						A.WONO, B.ITEM, ISNULL(C.DESCRIP, ''), A.SSN_NO, A.CURR_STATION, '', ''
		           FROM SFC_SSN A(NOLOCK), SFC_WO B(NOLOCK), SFC_ITEMS C(NOLOCK) 
		          WHERE A.WONO=?
		            AND A.CURR_STATION<>'SFCF'
		            AND A.RESULT='PASS'
		            AND A.WONO=B.WONO
		            AND B.ITEM=C.ITEM`,
				self.Usr.Conn, new(common.QueryDetail), s_wo)
		} else {
			wo_ls = *common.FetchAllRowsPtr(
				`SELECT TOP 10 ROW_NUMBER() OVER(ORDER BY A.SSN_NO) AS AUTOID, 
						A.WONO, B.ITEM, ISNULL(C.DESCRIP, ''), A.SSN_NO, A.CURR_STATION, '', ''
		           FROM SFC_SSN A(NOLOCK), SFC_WO B(NOLOCK), SFC_ITEMS C(NOLOCK) 
		          WHERE A.WONO=?
		            AND A.CURR_STATION=?
		            AND A.RESULT='PASS'
		            AND A.WONO=B.WONO
		            AND B.ITEM=C.ITEM`,
				self.Usr.Conn, new(common.QueryDetail), s_wo, s_st)
		}
	} else {
		if s_st == "All" {
			if s_im == "All" {
				wo_ls = *common.FetchAllRowsPtr(
					`SELECT TOP 10 ROW_NUMBER() OVER(ORDER BY A.SSN_NO) AS AUTOID, 
							A.WONO, B.ITEM, ISNULL(C.DESCRIP, ''), A.SSN_NO, A.CURR_STATION, '', ''
			           FROM SFC_SSN A(NOLOCK), SFC_WO B(NOLOCK), SFC_ITEMS C(NOLOCK) 
			          WHERE A.CURR_STATION<>'SFCF'
			            AND A.RESULT='PASS'
			            AND A.WONO=B.WONO
			            AND B.ITEM=C.ITEM`,
					self.Usr.Conn, new(common.QueryDetail))
			} else {
				wo_ls = *common.FetchAllRowsPtr(
					`SELECT TOP 10 ROW_NUMBER() OVER(ORDER BY A.SSN_NO) AS AUTOID, 
							A.WONO, B.ITEM, ISNULL(C.DESCRIP, ''), A.SSN_NO, A.CURR_STATION, '', ''
			           FROM SFC_SSN A(NOLOCK), SFC_WO B(NOLOCK), SFC_ITEMS C(NOLOCK) 
			          WHERE B.ITEM=?
			            AND A.CURR_STATION<>'SFCF'
			            AND A.RESULT='PASS'
			            AND A.WONO=B.WONO
			            AND B.ITEM=C.ITEM`,
					self.Usr.Conn, new(common.QueryDetail), s_im)
			}
		} else {
			if s_im == "All" {
				wo_ls = *common.FetchAllRowsPtr(
					`SELECT TOP 10 ROW_NUMBER() OVER(ORDER BY A.SSN_NO) AS AUTOID, 
							A.WONO, B.ITEM, ISNULL(C.DESCRIP, ''), A.SSN_NO, A.CURR_STATION, '', ''
			           FROM SFC_SSN A(NOLOCK), SFC_WO B(NOLOCK), SFC_ITEMS C(NOLOCK) 
			          WHERE A.CURR_STATION=?
			            AND A.RESULT='PASS'
			            AND A.WONO=B.WONO
			            AND B.ITEM=C.ITEM`,
					self.Usr.Conn, new(common.QueryDetail), s_st)
			} else {
				wo_ls = *common.FetchAllRowsPtr(
					`SELECT TOP 10 ROW_NUMBER() OVER(ORDER BY A.SSN_NO) AS AUTOID, 
							A.WONO, B.ITEM, ISNULL(C.DESCRIP, ''), A.SSN_NO, A.CURR_STATION, '', ''
			           FROM SFC_SSN A(NOLOCK), SFC_WO B(NOLOCK), SFC_ITEMS C(NOLOCK) 
			          WHERE B.ITEM=?
			            AND A.CURR_STATION=?
			            AND A.RESULT='PASS'
			            AND A.WONO=B.WONO
			            AND B.ITEM=C.ITEM`,
					self.Usr.Conn, new(common.QueryDetail), s_im, s_st)
			}
		}
	}

	rec_curr, _ := strconv.Atoi(s_nm)
	total_id := rec_curr / 10
	if rec_curr%10 > 0 {
		total_id++
	}

	self.TmplData["wo_ls"] = wo_ls
	self.TmplData["curr_page"] = "1"
	self.TmplData["total_page"] = total_id
	self.TmplData["workorder"] = s_wo
	self.TmplData["station"] = s_st
	self.TmplData["citem"] = s_im
}

func InprocessDetailHandler(ctx *web.Context) {
	self := new(QueryInprocessObj)
	self.Ctx = ctx
	self.Html = "inprocessdetail.html"
	self.Rightlist = []string{"querywo", "controlrun", "kanban"}
	self.DoQueryUrl(self)
}

func (self *InprocessPageObj) ParameterCheck() bool {
	pageid := self.Ctx.Params["pageid"] // current page
	s_wo := self.Ctx.Params["wo"]       // work order
	s_st := self.Ctx.Params["st"]       // station
	s_im := self.Ctx.Params["im"]       // item
	s_id, err := strconv.Atoi(pageid)
	if err != nil {
		return false
	}
	if s_wo == "" || s_st == "" || s_im == "" {
		return false
	}

	idx := 10 * (s_id - 1)

	self.Sqlparas = append(self.Sqlparas, s_wo)
	self.Sqlparas = append(self.Sqlparas, s_st)
	self.Sqlparas = append(self.Sqlparas, idx)
	self.Sqlparas = append(self.Sqlparas, s_im)

	return true
}

func (self *InprocessPageObj) SqlQueryExecute() {
	var wo_ls []interface{}
	s_wo := self.Sqlparas[0].(string)
	s_st := self.Sqlparas[1].(string)
	idx := self.Sqlparas[2].(int)
	s_im := self.Sqlparas[3].(string)
	if s_wo != "All" {
		if s_st == "All" {
			wo_ls = *common.FetchAllRowsPtr(
				`SELECT TOP 10 A.AUTOID, A.WONO, A.ITEM, ISNULL(A.DESCRIP, ''), A.SSN_NO, A.CURR_STATION, '', ''
	          	   FROM
	             		(
	                	SELECT ROW_NUMBER() OVER(ORDER BY A.SSN_NO) AS AUTOID, A.WONO, B.ITEM, 
	                			ISNULL(C.DESCRIP, '') AS DESCRIP, A.SSN_NO, A.CURR_STATION
		           		  FROM SFC_SSN A(NOLOCK), SFC_WO B(NOLOCK), SFC_ITEMS C(NOLOCK) 
		          		 WHERE A.WONO=?
		          		   AND A.CURR_STATION<>'SFCF'
		            	   AND A.RESULT='PASS'
		            	   AND A.WONO=B.WONO
		            	   AND B.ITEM=C.ITEM
	             		) A
	         	  WHERE A.AUTOID > ?`,
				self.Usr.Conn, new(common.QueryDetail), s_wo, idx)
		} else {
			wo_ls = *common.FetchAllRowsPtr(
				`SELECT TOP 10 A.AUTOID, A.WONO, A.ITEM, ISNULL(A.DESCRIP, ''), A.SSN_NO, A.CURR_STATION, '', ''
	          	   FROM
	             		(
	                	SELECT ROW_NUMBER() OVER(ORDER BY A.SSN_NO) AS AUTOID, A.WONO, B.ITEM, 
	                			ISNULL(C.DESCRIP, '') AS DESCRIP, A.SSN_NO, A.CURR_STATION
		           		  FROM SFC_SSN A(NOLOCK), SFC_WO B(NOLOCK), SFC_ITEMS C(NOLOCK) 
		          		 WHERE A.WONO=?
		          		   AND A.CURR_STATION=?
		            	   AND A.RESULT='PASS'
		            	   AND A.WONO=B.WONO
		            	   AND B.ITEM=C.ITEM
	             		) A
	         	  WHERE A.AUTOID > ?`,
				self.Usr.Conn, new(common.QueryDetail), s_wo, s_st, idx)
		}
	} else {
		if s_st == "All" {
			if s_im == "All" {
				wo_ls = *common.FetchAllRowsPtr(
					`SELECT TOP 10 A.AUTOID, A.WONO, A.ITEM, ISNULL(A.DESCRIP, ''), A.SSN_NO, A.CURR_STATION, '', ''
		          	   FROM
		             		(
		                	SELECT ROW_NUMBER() OVER(ORDER BY A.SSN_NO) AS AUTOID, A.WONO, B.ITEM, 
		                			ISNULL(C.DESCRIP, '') AS DESCRIP, A.SSN_NO, A.CURR_STATION
			           		  FROM SFC_SSN A(NOLOCK), SFC_WO B(NOLOCK), SFC_ITEMS C(NOLOCK) 
			          		 WHERE A.CURR_STATION<>'SFCF'
			            	   AND A.RESULT='PASS'
			            	   AND A.WONO=B.WONO
			            	   AND B.ITEM=C.ITEM
		             		) A
		         	  WHERE A.AUTOID > ?`,
					self.Usr.Conn, new(common.QueryDetail), idx)
			} else {
				wo_ls = *common.FetchAllRowsPtr(
					`SELECT TOP 10 A.AUTOID, A.WONO, A.ITEM, ISNULL(A.DESCRIP, ''), A.SSN_NO, A.CURR_STATION, '', ''
		          	   FROM
		             		(
		                	SELECT ROW_NUMBER() OVER(ORDER BY A.SSN_NO) AS AUTOID, A.WONO, B.ITEM, 
		                			ISNULL(C.DESCRIP, '') AS DESCRIP, A.SSN_NO, A.CURR_STATION
			           		  FROM SFC_SSN A(NOLOCK), SFC_WO B(NOLOCK), SFC_ITEMS C(NOLOCK) 
			          		 WHERE B.ITEM=?
			          		   AND A.CURR_STATION<>'SFCF'
			            	   AND A.RESULT='PASS'
			            	   AND A.WONO=B.WONO
			            	   AND B.ITEM=C.ITEM
		             		) A
		         	  WHERE A.AUTOID > ?`,
					self.Usr.Conn, new(common.QueryDetail), s_im, idx)
			}
		} else {
			if s_im == "All" {
				wo_ls = *common.FetchAllRowsPtr(
					`SELECT TOP 10 A.AUTOID, A.WONO, A.ITEM, ISNULL(A.DESCRIP, ''), A.SSN_NO, A.CURR_STATION, '', ''
		          	   FROM
		             		(
		                	SELECT ROW_NUMBER() OVER(ORDER BY A.SSN_NO) AS AUTOID, A.WONO, B.ITEM, 
		                			ISNULL(C.DESCRIP, '') AS DESCRIP, A.SSN_NO, A.CURR_STATION
			           		  FROM SFC_SSN A(NOLOCK), SFC_WO B(NOLOCK), SFC_ITEMS C(NOLOCK) 
			          		 WHERE A.CURR_STATION=?
			            	   AND A.RESULT='PASS'
			            	   AND A.WONO=B.WONO
			            	   AND B.ITEM=C.ITEM
		             		) A
		         	  WHERE A.AUTOID > ?`,
					self.Usr.Conn, new(common.QueryDetail), s_st, idx)
			} else {
				wo_ls = *common.FetchAllRowsPtr(
					`SELECT TOP 10 A.AUTOID, A.WONO, A.ITEM, ISNULL(A.DESCRIP, ''), A.SSN_NO, A.CURR_STATION, '', ''
		          	   FROM
		             		(
		                	SELECT ROW_NUMBER() OVER(ORDER BY A.SSN_NO) AS AUTOID, A.WONO, B.ITEM, 
		                			ISNULL(C.DESCRIP, '') AS DESCRIP, A.SSN_NO, A.CURR_STATION
			           		  FROM SFC_SSN A(NOLOCK), SFC_WO B(NOLOCK), SFC_ITEMS C(NOLOCK) 
			          		 WHERE B.ITEM=?
			          		   AND A.CURR_STATION=?
			            	   AND A.RESULT='PASS'
			            	   AND A.WONO=B.WONO
			            	   AND B.ITEM=C.ITEM
		             		) A
		         	  WHERE A.AUTOID > ?`,
					self.Usr.Conn, new(common.QueryDetail), s_im, s_st, idx)
			}
		}
	}

	dict_js, _ := json.Marshal(map[string]interface{}{"details": wo_ls, "lang": self.Lang})
	self.Ctx.ResponseWriter.Write(dict_js)
}

func SplitInprocessByPageHandler(ctx *web.Context) {
	self := new(InprocessPageObj)
	self.Ctx = ctx
	self.DoJsonQuery(self)
}

func (self *QueryInFailObj) ParameterCheck() bool {
	s_nm := self.Ctx.Params["nm"] // query result data number
	s_wo := self.Ctx.Params["wo"] // work order
	s_st := self.Ctx.Params["st"] // station
	s_im := self.Ctx.Params["im"] // item
	if s_wo == "" || s_st == "" || s_im == "" {
		return false
	}

	self.Sqlparas = append(self.Sqlparas, s_nm)
	self.Sqlparas = append(self.Sqlparas, s_wo)
	self.Sqlparas = append(self.Sqlparas, s_st)
	self.Sqlparas = append(self.Sqlparas, s_im)

	return true
}

func (self *QueryInFailObj) SqlQueryExecute() {
	s_nm := self.Sqlparas[0].(string)
	s_wo := self.Sqlparas[1].(string)
	s_st := self.Sqlparas[2].(string)
	s_im := self.Sqlparas[3].(string)
	var wo_ls []interface{}
	if s_wo != "All" {
		if s_st == "All" {
			wo_ls = *common.FetchAllRowsPtr(
				`SELECT TOP 50 ROW_NUMBER() OVER(ORDER BY SSN_NO), WONO, SSN_NO, CURR_STATION, 
						 RESULT, ISNULL(FAILCODE, ''), ISNULL(FAILDESC, ''), ''
				   FROM SFC_SSN(NOLOCK)
				  WHERE WONO=?
				    AND CURR_STATION<>'SFCF'
				    AND RESULT<>'PASS'`,
				self.Usr.Conn, new(common.WoFailInfo), s_wo)
		} else {
			wo_ls = *common.FetchAllRowsPtr(
				`SELECT TOP 50 ROW_NUMBER() OVER(ORDER BY SSN_NO), WONO, SSN_NO, CURR_STATION, 
						 RESULT, ISNULL(FAILCODE, ''), ISNULL(FAILDESC, ''), ''
				   FROM SFC_SSN(NOLOCK)
				  WHERE WONO=?
				    AND CURR_STATION=?
				    AND RESULT<>'PASS'`,
				self.Usr.Conn, new(common.WoFailInfo), s_wo, s_st)
		}
	} else {
		if s_st == "All" {
			if s_im == "All" {
				wo_ls = *common.FetchAllRowsPtr(
					`SELECT TOP 50 ROW_NUMBER() OVER(ORDER BY SSN_NO), WONO, SSN_NO, CURR_STATION, 
							 RESULT, ISNULL(FAILCODE, ''), ISNULL(FAILDESC, ''), ''
					   FROM SFC_SSN(NOLOCK)
					  WHERE CURR_STATION<>'SFCF'
					    AND RESULT<>'PASS'`,
					self.Usr.Conn, new(common.WoFailInfo))
			} else {
				wo_ls = *common.FetchAllRowsPtr(
					`SELECT TOP 50 ROW_NUMBER() OVER(ORDER BY A.SSN_NO), A.WONO, A.SSN_NO, A.CURR_STATION, 
							 A.RESULT, ISNULL(A.FAILCODE, ''), ISNULL(A.FAILDESC, ''), ''
					   FROM SFC_SSN A(NOLOCK), SFC_WO B(NOLOCK)
					  WHERE B.ITEM=?
					    AND A.CURR_STATION<>'SFCF'
					    AND A.RESULT<>'PASS'
					    AND A.WONO=B.WONO`,
					self.Usr.Conn, new(common.WoFailInfo), s_im)
			}
		} else {
			if s_im == "All" {
				wo_ls = *common.FetchAllRowsPtr(
					`SELECT TOP 50 ROW_NUMBER() OVER(ORDER BY SSN_NO), WONO, SSN_NO, CURR_STATION, 
							 RESULT, ISNULL(FAILCODE, ''), ISNULL(FAILDESC, ''), ''
					   FROM SFC_SSN(NOLOCK)
					  WHERE CURR_STATION=?
					    AND RESULT<>'PASS'`,
					self.Usr.Conn, new(common.WoFailInfo), s_st)
			} else {
				wo_ls = *common.FetchAllRowsPtr(
					`SELECT TOP 50 ROW_NUMBER() OVER(ORDER BY A.SSN_NO), A.WONO, A.SSN_NO, A.CURR_STATION, 
							 A.RESULT, ISNULL(A.FAILCODE, ''), ISNULL(A.FAILDESC, ''), ''
					   FROM SFC_SSN A(NOLOCK), SFC_WO B(NOLOCK)
					  WHERE B.ITEM=?
					    AND A.CURR_STATION=?
					    AND A.RESULT<>'PASS'
					    AND A.WONO=B.WONO`,
					self.Usr.Conn, new(common.WoFailInfo), s_im, s_st)
			}
		}
	}

	rec_curr, _ := strconv.Atoi(s_nm)
	total_id := rec_curr / 50
	if rec_curr%50 > 0 {
		total_id++
	}

	self.TmplData["wo_ls"] = wo_ls
	self.TmplData["curr_page"] = "1"
	self.TmplData["total_page"] = total_id
	self.TmplData["workorder"] = s_wo
	self.TmplData["station"] = s_st
	self.TmplData["citem"] = s_im
}

func InFailDetailHandler(ctx *web.Context) {
	self := new(QueryInFailObj)
	self.Ctx = ctx
	self.Html = "infaildetail.html"
	self.Rightlist = []string{"querywo", "controlrun", "kanban"}
	self.DoQueryUrl(self)
}

func (self *InFailPageObj) ParameterCheck() bool {
	pageid := self.Ctx.Params["pageid"] // current page
	s_wo := self.Ctx.Params["wo"]       // work order
	s_st := self.Ctx.Params["st"]       // station
	s_im := self.Ctx.Params["im"]       // item
	s_id, err := strconv.Atoi(pageid)
	if err != nil {
		return false
	}
	if s_wo == "" || s_st == "" || s_im == "" {
		return false
	}

	idx := 50 * (s_id - 1)

	self.Sqlparas = append(self.Sqlparas, s_wo)
	self.Sqlparas = append(self.Sqlparas, s_st)
	self.Sqlparas = append(self.Sqlparas, idx)
	self.Sqlparas = append(self.Sqlparas, s_im)

	return true
}

func (self *InFailPageObj) SqlQueryExecute() {
	var wo_ls []interface{}
	s_wo := self.Sqlparas[0].(string)
	s_st := self.Sqlparas[1].(string)
	idx := self.Sqlparas[2].(int)
	s_im := self.Sqlparas[3].(string)
	if s_wo != "All" {
		if s_st == "All" {
			wo_ls = *common.FetchAllRowsPtr(
				`SELECT TOP 50 A.AUTOID, A.WONO, A.SSN_NO, A.CURR_STATION, A.RESULT, A.FAILCODE, 
						 A.FAILDESC, ''
	          	   FROM
	             		(
	                	SELECT ROW_NUMBER() OVER(ORDER BY SSN_NO) AS AUTOID, WONO, SSN_NO, CURR_STATION, 
						 		RESULT, ISNULL(FAILCODE, '') AS FAILCODE, ISNULL(FAILDESC, '') AS FAILDESC
				   		  FROM SFC_SSN(NOLOCK)
				  		 WHERE WONO=?
				    	   AND CURR_STATION<>'SFCF'
				    	   AND RESULT<>'PASS'
	             		) A
	         	  WHERE A.AUTOID > ?`,
				self.Usr.Conn, new(common.WoFailInfo), s_wo, idx)
		} else {
			wo_ls = *common.FetchAllRowsPtr(
				`SELECT TOP 50 A.AUTOID, A.WONO, A.SSN_NO, A.CURR_STATION, A.RESULT, A.FAILCODE, 
						 A.FAILDESC, ''
	          	   FROM
	             		(
	                	SELECT ROW_NUMBER() OVER(ORDER BY SSN_NO) AS AUTOID, WONO, SSN_NO, CURR_STATION, 
						 		RESULT, ISNULL(FAILCODE, '') AS FAILCODE, ISNULL(FAILDESC, '') AS FAILDESC
				   		  FROM SFC_SSN(NOLOCK)
				  		 WHERE WONO=?
				    	   AND CURR_STATION=?
				    	   AND RESULT<>'PASS'
	             		) A
	         	  WHERE A.AUTOID > ?`,
				self.Usr.Conn, new(common.WoFailInfo), s_wo, s_st, idx)
		}
	} else {
		if s_st == "All" {
			if s_im == "All" {
				wo_ls = *common.FetchAllRowsPtr(
					`SELECT TOP 50 A.AUTOID, A.WONO, A.SSN_NO, A.CURR_STATION, A.RESULT, A.FAILCODE, 
							 A.FAILDESC, ''
		          	   FROM
		             		(
		                	SELECT ROW_NUMBER() OVER(ORDER BY SSN_NO) AS AUTOID, WONO, SSN_NO, CURR_STATION, 
							 		RESULT, ISNULL(FAILCODE, '') AS FAILCODE, ISNULL(FAILDESC, '') AS FAILDESC
					   		  FROM SFC_SSN(NOLOCK)
					  		 WHERE CURR_STATION<>'SFCF'
					    	   AND RESULT<>'PASS'
		             		) A
		         	  WHERE A.AUTOID > ?`,
					self.Usr.Conn, new(common.WoFailInfo), idx)
			} else {
				wo_ls = *common.FetchAllRowsPtr(
					`SELECT TOP 50 A.AUTOID, A.WONO, A.SSN_NO, A.CURR_STATION, A.RESULT, A.FAILCODE, 
							 A.FAILDESC, ''
		          	   FROM
		             		(
		                	SELECT ROW_NUMBER() OVER(ORDER BY A.SSN_NO) AS AUTOID, A.WONO, A.SSN_NO, A.CURR_STATION, 
							 		A.RESULT, ISNULL(A.FAILCODE, '') AS FAILCODE, ISNULL(A.FAILDESC, '') AS FAILDESC
					   		  FROM SFC_SSN A(NOLOCK), SFC_WO B(NOLOCK)
					  		 WHERE B.ITEM=?
					    	   AND A.CURR_STATION<>'SFCF'
					    	   AND A.RESULT<>'PASS'
					    	   AND A.WONO=B.WONO
		             		) A
		         	  WHERE A.AUTOID > ?`,
					self.Usr.Conn, new(common.WoFailInfo), s_im, idx)
			}
		} else {
			if s_im == "All" {
				wo_ls = *common.FetchAllRowsPtr(
					`SELECT TOP 50 A.AUTOID, A.WONO, A.SSN_NO, A.CURR_STATION, A.RESULT, A.FAILCODE, 
							 A.FAILDESC, ''
		          	   FROM
		             		(
		                	SELECT ROW_NUMBER() OVER(ORDER BY SSN_NO) AS AUTOID, WONO, SSN_NO, CURR_STATION, 
							 		RESULT, ISNULL(FAILCODE, '') AS FAILCODE, ISNULL(FAILDESC, '') AS FAILDESC
					   		  FROM SFC_SSN(NOLOCK)
					  		 WHERE CURR_STATION=?
					    	   AND RESULT<>'PASS'
		             		) A
		         	  WHERE A.AUTOID > ?`,
					self.Usr.Conn, new(common.WoFailInfo), s_st, idx)
			} else {
				wo_ls = *common.FetchAllRowsPtr(
					`SELECT TOP 50 A.AUTOID, A.WONO, A.SSN_NO, A.CURR_STATION, A.RESULT, A.FAILCODE, 
							 A.FAILDESC, ''
		          	   FROM
		             		(
		                	SELECT ROW_NUMBER() OVER(ORDER BY A.SSN_NO) AS AUTOID, A.WONO, A.SSN_NO, A.CURR_STATION, 
							 		A.RESULT, ISNULL(A.FAILCODE, '') AS FAILCODE, ISNULL(A.FAILDESC, '') AS FAILDESC
					   		  FROM SFC_SSN A(NOLOCK), SFC_WO B(NOLOCK)
					  		 WHERE B.ITEM=?
					  		   AND A.CURR_STATION=?
					    	   AND A.RESULT<>'PASS'
					    	   AND A.WONO=B.WONO
		             		) A
		         	  WHERE A.AUTOID > ?`,
					self.Usr.Conn, new(common.WoFailInfo), s_im, s_st, idx)
			}
		}
	}

	dict_js, _ := json.Marshal(map[string]interface{}{"details": wo_ls, "lang": self.Lang})
	self.Ctx.ResponseWriter.Write(dict_js)
}

func SplitInFailByPageHandler(ctx *web.Context) {
	self := new(InFailPageObj)
	self.Ctx = ctx
	self.DoJsonQuery(self)
}
